<?php
class SiteService
{
	public function getSiteComment($num = 5)
	{
		$list = array();
		
		$res = DB::query('SELECT s.id AS site_id,m.title,s.name AS site_name 
						FROM '.DB::table('site').' AS s 
						INNER JOIN '.DB::table('message').' AS m ON m.rec_id = s.id AND m.status = 1 
							AND m.rec_module = \'SiteMessage\' AND m.create_time = s.last_mtime 
						WHERE s.status = 1 GROUP BY s.id ORDER BY s.last_mtime DESC,s.sort ASC LIMIT 0,'.$num);
		
		while($data = DB::fetch($res))
		{
			$data['site_url'] = U("site/show",array("id"=>$data['site_id']));
			$list[] = $data;
		}

		return $list;
	}
	
	public function getUrlExists($url)
	{
		$url = getUrlHost($url);
		if(intval(DB::resultFirst('SELECT COUNT(id) FROM '.DB::table('site')." WHERE (LOCATE('$url',url) > 0 OR LOCATE(url,'$url') > 0) AND url <> ''")) > 0)
			return true;
		else
			return false;
	}
	
	public function getSiteByUserID($uid)
	{
		return DB::fetchFirst('SELECT id,name,content,site_img FROM '.DB::table('site')." WHERE user_id = '$uid'");
	}
	
	public function updateSiteByUserID($id,$uid,$site)
	{
		DB::update('site',$site,"user_id = '$uid' AND id = '$id'");
	}
}
?>